package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询.参数{}",categoryPageQueryDTO);
        PageResult pageResult = categoryService.page(categoryPageQueryDTO);
        return Result.success(pageResult);
    }
    @PostMapping
    public Result add(@RequestBody CategoryDTO categoryDTO){
        log.info("添加分类.参数{}",categoryDTO);
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryService.add(category);
        return Result.success();
    }
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("启用或禁用分类.状态{},id{}",status,id);
        categoryService.status(status,id);
        return Result.success();
    }
    @DeleteMapping
    public Result delete(Long id){
        log.info("删除分类.id{}",id);
        categoryService.delete(id);
        return Result.success();
    }
    @GetMapping("/list")
    public Result<List<Category>> getByType( Integer  type){
        log.info("查询分类.type{}",type);
        List<Category> list = categoryService.getByType(type);
        return Result.success(list);
    }
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类.参数{}",categoryDTO);
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryService.update(category);
        return Result.success();
    }
}
